package cn.zcy.pool;

import cn.zcy.conno.MyConnection2;
import cn.zcy.untils.JDBCUtils;

import javax.sql.DataSource;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.logging.Logger;

public class MyDataSource implements DataSource {
    //定义容器保存连接

    // 定义容器保存连接
    private static ConcurrentLinkedQueue<Connection> pool = new ConcurrentLinkedQueue<>();


    //通过工具类获取十个连接，保存在连接池中
    static {
        for (int i = 0; i < 10; i++) {
            pool.add(JDBCUtils.getConnection());
        }
    }

    //获取连接
    @Override
    public Connection getConnection() throws SQLException {
        if (pool.size() > 0) {
            Connection connection = pool.poll();

            MyConnection2 myConnection2 = new MyConnection2();
            Connection myConnection22 = myConnection2.getMyConnection2(connection, pool);
            return myConnection22;
        } else {
            throw new RuntimeException("连接已用尽");

        }


    }

    //返回连接池容器大小

    public int getSize() {
        return pool.size();
    }

    @Override
    public Connection getConnection(String username, String password) throws SQLException {
        return null;
    }

    @Override
    public <T> T unwrap(Class<T> iface) throws SQLException {
        return null;
    }

    @Override
    public boolean isWrapperFor(Class<?> iface) throws SQLException {
        return false;
    }

    @Override
    public PrintWriter getLogWriter() throws SQLException {
        return null;
    }

    @Override
    public void setLogWriter(PrintWriter out) throws SQLException {

    }

    @Override
    public void setLoginTimeout(int seconds) throws SQLException {

    }

    @Override
    public int getLoginTimeout() throws SQLException {
        return 0;
    }

    @Override
    public Logger getParentLogger() throws SQLFeatureNotSupportedException {
        return null;
    }
}
